package com.ychong.library.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.os.Build
import android.text.TextPaint
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.View
import com.ychong.library.R
import com.ychong.library.utils.ResUtils
import com.ychong.library.utils.ScreenUtils

class YCHPlateInputView : View {
    private var num: Int = 8
    private var mWidth: Float = 0f
    private var mHeight: Float = 0f
    private var mSpace: Float = 0f
    private var borderWidth: Float = 1f
    private var radiusSize: Float = 0f
    private var borderColor: Int? = null
    private var inputBorderColor: Int? = null
    private var mTextColor: Int? = null
    private var mTextSize: Float? = null
    private var paint: Paint? = null
    private var textPain: TextPaint? = null
    private var plateNoList: MutableList<Char> = ArrayList(8)
    private var start: Float = 0f

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : this(context, attributeSet, 0)

    @SuppressLint("Recycle")
    constructor(context: Context, attributeSet: AttributeSet?, defStyle: Int) : super(
        context,
        attributeSet,
        defStyle
    ) {
        val style = context.obtainStyledAttributes(attributeSet, R.styleable.YCHPlateInputView)
        num = style.getInt(R.styleable.YCHPlateInputView_pivNumber, 8)
        mWidth = style.getDimensionPixelSize(
            R.styleable.YCHPlateInputView_pivWidth,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40f, resources.displayMetrics)
                .toInt()
        ).toFloat()
        mHeight = style.getDimensionPixelSize(
            R.styleable.YCHPlateInputView_pivHeight,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50f, resources.displayMetrics)
                .toInt()
        ).toFloat()
        mSpace = style.getDimensionPixelSize(
            R.styleable.YCHPlateInputView_pivPadding,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5f, resources.displayMetrics)
                .toInt()
        ).toFloat()
        radiusSize = style.getDimensionPixelSize(
            R.styleable.YCHPlateInputView_pivBorderRadiusSize,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5f, resources.displayMetrics)
                .toInt()
        ).toFloat()
        borderWidth = style.getDimensionPixelSize(
            R.styleable.YCHPlateInputView_pivBorderWidth,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2f, resources.displayMetrics)
                .toInt()
        ).toFloat()
        borderColor = style.getColor(R.styleable.YCHPlateInputView_pivBorderColor, Color.BLUE)
        inputBorderColor =
            style.getColor(R.styleable.YCHPlateInputView_pivInputBorderColor, Color.RED)
        mTextColor = style.getColor(R.styleable.YCHPlateInputView_pivTextColor, Color.BLUE)
        mTextSize = style.getDimensionPixelSize(
            R.styleable.YCHPlateInputView_pivTextSize,
            TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16f, resources.displayMetrics)
                .toInt()
        ).toFloat()
        init()
    }

    private fun init() {
        paint = Paint()
        paint!!.isAntiAlias = true //抗锯齿
        paint!!.style = Paint.Style.STROKE
        paint!!.strokeWidth = borderWidth
        textPain = TextPaint()
        textPain!!.isAntiAlias = true //抗锯齿

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)

    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val w = num * mWidth + (num + 1) * mSpace
        if (w <= width) {
            start = (width - w) / 2
        } else {
            mWidth = (mWidth * (width / w * 1f))
            mSpace = (mSpace * (width / w * 1f))
            start = 0f
        }
        textPain!!.textSize = mTextSize!!
        for (index in 0 until num) {
            val left = start + index * mWidth + (index + 1) * mSpace
            val top = 0f + borderWidth / 2
            val right = left + mWidth
            val bottom = mHeight - borderWidth / 2

            drawBox(canvas, left, top, right, bottom, index)
            drawText(canvas, left, top, right, bottom, index)
        }
    }

    private fun drawBox(
        canvas: Canvas?,
        left: Float,
        top: Float,
        right: Float,
        bottom: Float,
        index: Int
    ) {
        if (index == 7) {
            val floatArr: FloatArray = floatArrayOf(4f, 4f)
            paint!!.pathEffect = DashPathEffect(floatArr, 0f)
            if (plateNoList.size == index) {
                paint!!.color = inputBorderColor!!
            } else {
                paint!!.color = ResUtils.getColor(context, R.color.color_0ABE34)
            }
        } else {
            paint!!.pathEffect = null
            if (plateNoList.size == index) {
                paint!!.color = inputBorderColor!!
            } else {
                paint!!.color = borderColor!!
            }
        }
        //画框
        if (Build.VERSION.SDK_INT >= 21) {
            canvas!!.drawRoundRect(
                left,
                top,
                right,
                bottom,
                radiusSize,
                radiusSize,
                paint!!
            )
        } else {
            val rectF = RectF(
                left,
                top,
                right,
                bottom
            )
            canvas!!.drawRoundRect(rectF, radiusSize, radiusSize, paint!!)

        }
    }

    private fun drawText(
        canvas: Canvas?,
        left: Float,
        top: Float,
        right: Float,
        bottom: Float,
        index: Int
    ) {
        if (index == 7) {
            textPain!!.color = ResUtils.getColor(context, R.color.color_0ABE34)
        }else{
            textPain!!.color = mTextColor!!
        }
        if (index == 7 && plateNoList.size < 8) {
            val text = "新能源"
            val charArr = text.toCharArray()
            textPain!!.textSize = ScreenUtils.spToPx(10f).toFloat()
            val pfm = textPain!!.fontMetrics
            val textHeight = pfm.descent - pfm.ascent
            val totalHeight = textHeight * charArr.size
            val start = (mHeight - totalHeight) / 2f
            val size = charArr.size
            for (i in 0 until size) {
                val charStr = charArr[i].toString()
                val textWidth = textPain!!.measureText(charStr)
                val x = (right + left - textWidth) / 2
                val y = start-(textHeight/size)+textHeight*(i+1)
                Log.e("ddd","$y    $start  $mHeight   $totalHeight   $textHeight")
                canvas!!.drawText(
                    charStr,
                    x,
                    y,
                    textPain!!
                )
            }

        } else {
            if (plateNoList.size != 0) {
                //文字
                val charStr = if (index < plateNoList.size) {
                    plateNoList[index].toString()
                } else {
                    ""
                }
                val textWidth = textPain!!.measureText(charStr)
                val pfm = textPain!!.fontMetrics
                val textHeight = pfm.ascent + pfm.descent
                canvas!!.drawText(
                    charStr,
                    (right + left - textWidth) / 2,
                    (top + bottom - textHeight) / 2,
                    textPain!!
                )
            }
        }
    }

    fun input(text: String) {
        plateNoList.clear()
        for (char in text.toCharArray()) {
            plateNoList.add(char)
        }
        invalidate()
    }
}